﻿namespace ScrumLite.WebUx.SharedViews
{
    using System;
    using System.Collections.ObjectModel;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using ScrumLite.WebUx.Models;

    public abstract class BaseViewMasterPage<TViewData> : ViewMasterPage<TViewData> where TViewData : class
    {
        public new TViewData ViewData
        {
            get
            {
                IViewDataContainer page = this.Page as IViewDataContainer;
                TViewData d = page.ViewData as TViewData;
                if (null == d)
                {
                    throw new InvalidOperationException(Properties.Resources.NotCompatibleViewdata);
                }

                return d;
            }
        }
    }

    public class YSlowCompatibleMasterPage : BaseViewMasterPage<ContextViewData>
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!HttpContext.Current.IsDebuggingEnabled)
            {
                Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
            }
        }

        public new BaseHtmlHelper Html { get; private set; }
        public new BaseUrlHelper Url { get; private set; }

        protected override void OnLoad(EventArgs e)
        {
            Html = new BaseHtmlHelper(ViewContext);
            Url = new BaseUrlHelper(ViewContext);
            base.OnLoad(e);
        }

        protected virtual void CombineCss()
        {
            Collection<HtmlControl> stylesheets = new Collection<HtmlControl>();
            foreach (Control control in Page.Header.Controls)
            {
                HtmlControl c = control as HtmlControl;

                if (c != null && c.Attributes["rel"] != null && c.Attributes["rel"].Equals("stylesheet", StringComparison.OrdinalIgnoreCase))
                {
                    if (!c.Attributes["href"].StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                        stylesheets.Add(c);
                }
            }

            string[] paths = new string[stylesheets.Count];
            for (int i = 0; i < stylesheets.Count; i++)
            {
                Page.Header.Controls.Remove(stylesheets[i]);
                paths[i] = stylesheets[i].Attributes["href"];
            }

            this.AddStylesheetsToHeader(paths);
        }

        private void AddStylesheetsToHeader(string[] paths)
        {
            if (paths.Length > 0)
            {
                string basePath = paths[0].Substring(0, paths[0].LastIndexOf('/'));
                if (basePath.Length != 0) basePath += "/";

                HtmlLink link = new HtmlLink();
                link.Attributes["rel"] = "stylesheet";
                link.Attributes["type"] = "text/css";
                link.Href = basePath + "CssCombine.ashx?css=" + Server.UrlEncode(string.Join(",", paths));
                Page.Header.Controls.Add(link);
            }
        }

        private void Page_PreRenderComplete(object sender, EventArgs e)
        {
            this.CombineCss();
        }
    }
}
